  //这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
  //例如：import 《组件名称》 from '《组件路径》';
  import { scrollMixin } from "utils/mixin";
export default {
  name:'',
  //import引入的组件需要注入到对象中才能使用
  components: {},
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      activeNames:[''],
      id:'',
      chapterId:'', //章节ID
      sectionId:'', //小节id
      courseId:'', //课程ID
      goodsId:'',
      optionId:'',
      videoUrl:'',
      deadline:'',
      chapterData: [],
      chapterInfo:{}, //章节详情
      chapterCount:0,
      sectionCount:0,
      subchapterIndex:0,
      chapterIndex:0,
      page:1,
      videoSecond:0,
      videoShow:false,
      isLoadMore: true,
      total_page: 0,
      total:1,
      courseStatus:'',
      timer:0,
      canTry:0, //是能否试看 0 否 1是
      pluginSetting:{}, //插件设置
      iPnoneBottomBol: false, //是否是苹果手机
    };
  },
  //监听属性 类似于data概念
  computed: {
    videoSecondTime() {
      return function(second) {
        let minute = parseInt(second / 60);
        let secondTime = second % 60;
        if(secondTime != 0 || minute !=0) {
          return `${minute}分${secondTime}秒`;
        }
      }
    },
  },
  //监控data中的数据变化
  //方法集合
  methods: {
    backClick() {
      if (window.history.length <= 1) {
          this.$router.push(this.fun.getUrl("home"));
          return false
      } else {
          this.$router.go(-1)
      }
    },
    sectionOn(sectionItem,sectionIndex,index) {
      let {section_id,can_try} = {...sectionItem};
      this.canTry = can_try;
      if(this.courseStatus === 0) {
        this.videoSecond = sectionItem.try_time;
      } else {
        this.videoSecond = sectionItem.video_second;
      }
      
      if((sectionItem.can_try && this.courseStatus === 0) || this.courseStatus === 1) {
        this.chapterId = this.chapterData[index].chapter_id;
        this.sectionId = section_id;
        this.subchapterIndex = sectionIndex;
        this.chapterIndex = index;
        this.videoUrlData();
      } else {
        this.videoShow = true;
      }                                                            
     
    },
    // 获取当前播放位置
    timeupdate(event) {
      if(parseInt(event.target?.currentTime) > 0) {
        if(this.courseStatus === 0) {
          this.videoShow = true;
          this.$refs.myVideo.pause();
        }
      }
      
    },
    videoTimeData(event) {
      if(!this.canTry && this.courseStatus === 0) {
        this.videoShow = true;
        this.$refs.myVideo.pause();
      }
    },
    videoPlay() {
      setTimeout(() => {
        this.$router.push(
          this.fun.getUrl(
            "goodsorder",
            {},
            {
              goodsId: this.chapterInfo.goods_id,
              optionsId: this.optionId,
              total: this.total,
              tag: "course",
            }
          )
        );
      },500);
    },
    chapterList() {
      $http.post("plugin.course-supply.api.course.chapter-list", {id : this.id,page:this.page}, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          this.isLoadMore = true;
          this.total_page = res.last_page;
          if (!this.total_page) {
            this.page = 0;
          }
          this.sectionCount = res.section_count;
          this.chapterCount = res.chapter_count;
          this.chapterData = res.data;
          this.activeNames[0] = this.chapterData[0]?.id;
         
          this.canTry = Number(this.chapterData[0]?.section[0]?.can_try);
          this.courseStatus = res.course_status; //购买状态
          if(this.courseStatus === 0) {
            this.videoSecond = Number(this.chapterData[0]?.section[0]?.try_time);
          } else {
            this.videoSecond = Number(this.chapterData[0]?.section[0]?.video_second);
          }
          if(!this.courseId) {
            this.courseId = this.chapterData[0]?.course_id;
          }
          if(!this.chapterId) {
            this.chapterId = this.chapterData[0]?.chapter_id;
          }
          if(!this.sectionId) {
            this.sectionId = this.chapterData[0]?.section[0]?.section_id;
          }
          this.deadline = this.timestampToTime(res.deadline);
          this.videoUrlData();
        } else {
          this.page = this.page - 1;
          this.isLoadMore = false;
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    //获取更多数据
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page = this.page + 1;
        $http.post("plugin.course-supply.api.course.chapter-list", {id : this.id,page:this.page}, "加载中").then(
          res => {
            this.isLoadMore = true;
            if (res.result === 1) {
              let myData = res.data?.data;
              this.chapterData = [...this.chapterData, ...myData]; //数组拼接
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
            }
          },
          function(response) {
            console.log("response", response);
            // error callback
          }
        );
      }
    },
    //日期转化
    timestampToTime(timestamp) {
      var date = new Date(timestamp * 1000); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
      let Y = date.getFullYear() + "-";
      let M =
        (date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1)
          : date.getMonth() + 1) + "-";
      let D = this.change(date.getDate()) + " ";
      let h = this.change(date.getHours()) + ":";
      let m = this.change(date.getMinutes())+ ":";
      let s = this.change(date.getSeconds());
      return Y + M + D + h + m + s;
    },
    change(t) {
      if (t < 10) {
        return "0" + t;
      } else {
        return t;
      }
    },
    courseInfo() {
      $http.post("plugin.course-supply.api.course.info", {id : this.id,basic_info:1}, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          this.chapterInfo = res;
          this.pluginSetting = response.basic_info?.plugin_setting;
          this.$nextTick(() => {
            this.fun.wxShare(
              "",
              { mid: this.fun.getKeyByMid()},
              {
                link: window.location.href,
                title: this.chapterInfo.name,
                imgUrl: this.chapterInfo?.cover,
                description: this.chapterInfo?.content
              }
            );
          },50);
          
        } else {
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
    videoUrlData() {
      let params = {
        course_id: this.courseId,
        chapter_id: this.chapterId,
        section_id: this.sectionId
      };
      $http.post("plugin.course-supply.api.course.get-video-url", params, "加载中..").then(response => {
        if (response.result == 1) {
          let res = response.data;
          let data = res.data;
          this.videoUrl = data?.url;
          this.optionId = res?.option_id;
        } else {
          this.$dialog.alert({
            message: response.msg
          });
        }
      });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.id = this.$route.params.chapterId;
    this.chapterId = this.$route.params.videoId;
    this.courseId = this.$route.params.courseId;
    this.sectionId = this.$route.params.sectionId;
    this.courseInfo();
    this.chapterList();

  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
     // 监听视频播放
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {
    
  }, //如果页面有keep-alive缓存功能，这个函数会触发
}